from django import forms

from user.models import *


class RegAndLogForm(forms.ModelForm):
    # 用于验证和处理用户注册和登录的表单类
    name = forms.CharField(max_length=10, min_length=2, label='用户名', widget=forms.TextInput, required=True)
    password = forms.CharField(max_length=16, min_length=2, label='密码', widget=forms.PasswordInput, required=True)
    code = forms.CharField(label='验证码', required=True,)
    # 用于与数据库交互，存储数据
    class Meta:
        model = Users
        fields = ['name', 'password']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            field.widget.attrs = {"class": "form-control"}

    # 绕过数据库中对name的唯一性验证
    def validate_unique(self):
        pass

# class RegisterAndLoginForm(forms.Form):
#     name = forms.CharField(label='名称', min_length=3, max_length=100, required=True,
#                            widget=forms.TextInput(
#                                attrs={'class': 'username-input', 'id': 'name', 'placeholder': '请输入用户名'}),
#                            error_messages={'required': '用户名', 'max_length': '用户名长度不能超过100个字符',
#                                            'min_length': '用户名长度不能少于2个字符'})
#     password = forms.CharField(label='密码', min_length=6, max_length=100, required=True,
#                                widget=forms.PasswordInput(
#                                    attrs={'class': 'password-input', 'id': 'password', 'placeholder': '请输入密码'}, ),
#                                error_messages={'required': '密码必须填写', 'max_length': '用户名长度不能超过100个字符',
#                                                'min_length': '用户名长度不能少于2个字符'
#                                                })
#     verify_password = forms.CharField(label='确认密码', min_length=6, max_length=100, required=True,
#                                       widget=forms.PasswordInput(
#                                           attrs={'class': 'password-input', 'id': 'verify_password',
#                                                  'placeholder': '请确认密码'}))
#
#     def clean(self):
#         cleaned_data = super().clean()
#         password = cleaned_data.get("password")
#         verify_password = cleaned_data.get("verify_password")
#
#         if password and verify_password and password != verify_password:
#             self.add_error('verify_password', "两次输入的密码不一致")  # 错误关联到指定字段
#         return cleaned_data
